//
//  WBTabbar.m
//  WeatherBar
//
//  Created by yby on 2024/6/7.
//

#import "WBTabbar.h"

@implementation WBTabbar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.barStyle = UIBarStyleDefault;
        self.translucent = YES;

        self.backgroundColor = [UIColor whiteColor];
        self.var_centerBtn = [self wb_addCenterBtn];
        [self addSubview:self.var_centerBtn];
        self.clipsToBounds = NO;
    }
    return self;
}

- (UIView *)wb_addCenterBtn{
    UIView *var_view = [[UIView alloc] init];
    var_view.bounds = CGRectMake(0, 0, 76, 76);
    var_view.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.3);
    UIImageView *var_centerItem = [[UIImageView alloc] init];
    var_centerItem.image = [UIImage imageNamed:@"tabbar_add"];
    var_centerItem.frame = var_view.bounds;
    [var_view addSubview:var_centerItem];

    return var_view;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat var_offset = 0;
    if (kIsBangsScreen) {
        var_offset = 10;
        self.var_centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, -24);
    }else{
        self.var_centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.35);
    }

    int vae_index = 0;
    CGFloat var_wigth = self.bounds.size.width / 3;
    UIView *var_barBackgroundView;
    for (UIView* var_sub in self.subviews) {
        
        if ([var_sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            var_sub.frame = CGRectMake(vae_index * var_wigth, self.bounds.origin.y-var_offset, var_wigth, self.bounds.size.height - 2);
            vae_index++;
            
            if (vae_index == 1) {
                vae_index++;
            }
        }
    }
    
    for (UIView*var_imgView in var_barBackgroundView.subviews) {
        if (var_imgView.wb_height == 0.5) {
            var_imgView.backgroundColor = [UIColor clearColor];
        }
    }
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (self.isHidden == NO) {
        
        CGPoint var_newPoint = [self convertPoint:point toView:self.var_centerBtn];
        
        if ( [self.var_centerBtn pointInside:var_newPoint withEvent:event]) {
            return self.var_centerBtn;
        }else{
            
            return [super hitTest:point withEvent:event];
        }
    }
    else {
        return [super hitTest:point withEvent:event];
    }
}
- (UIImage *)wb_scaleImage:(UIImage *)image toScale:(float)scaleSize {
    UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *var_scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return var_scaledImage;
}


@end
